Imports DaveSexton.DocProject.DocSites

Partial Public Class _Default
	Inherits System.Web.UI.Page
	Implements IDocSiteDefault

	Private Sub SyncSidebar()
		Dim topic As String = Nothing
		Dim helpFile As String = Nothing

		If Not Page.IsPostBack Then
			If Not Request.QueryString("filenotfound") Is Nothing Then
				ContentPath = DocSiteManager.Settings.FileNotFoundPath
			Else
				topic = Request.QueryString("topic")
				helpFile = Request.QueryString("helpfile")

				If Not String.IsNullOrEmpty(topic) Then
					topic = DocSiteNavigator.FormatTopic(topic, False)
				End If
			End If
		Else
			Dim topicPath As String = ContentUrl.Value

			If Not String.IsNullOrEmpty(topicPath) Then
				Dim topicUri As Uri = Nothing

				If Uri.TryCreate(topicPath, UriKind.RelativeOrAbsolute, topicUri) AndAlso _
				 (Not topicUri.IsAbsoluteUri OrElse topicUri.Host.Equals(Request.Url.Host, StringComparison.OrdinalIgnoreCase)) Then
					If topicUri.IsAbsoluteUri Then
						helpFile = topicUri.AbsolutePath
					Else
						helpFile = topicUri.ToString()
					End If

					If helpFile.StartsWith("/") OrElse helpFile.StartsWith("\") Then
						helpFile = helpFile.Substring(1)
					End If
				End If
			End If
		End If

		If String.IsNullOrEmpty(helpFile) Then
			helpFile = DocSiteNavigator.ResolveTopicHelpFile(topic, False)

			If helpFile Is Nothing Then
				helpFile = DocSiteManager.Settings.HelpFileNotFoundPath
			End If
		End If

		If String.IsNullOrEmpty(topic) Then
			topic = DocSiteNavigator.ResolveHelpFileTopic(helpFile, False)

			If topic Is Nothing Then
				helpFile = DocSiteManager.Settings.HelpFileNotFoundPath
			End If
		End If

		ContentPath = helpFile

		Dim sidebar As DocSiteSidebar = CType(Master, DocSite).Sidebar

		If Not sidebar.TableOfContents.SelectedTopic.Equals(topic, StringComparison.Ordinal) Then
			sidebar.Initialize(topic, helpFile)
		End If
	End Sub

	Protected Function GetSyncTocClientCallback() As String
		Return CType(Master, DocSite).Sidebar.TableOfContents.GetSyncTocClientCallback()
	End Function

	Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
		MyBase.OnLoad(e)

		SyncSidebar()

		Dim script As String = _
		"function ContentFrame_onload(eventObj)" & vbCrLf & _
		"{" & vbCrLf & _
		"	if (!contentFrame_FirstLoad)" & vbCrLf & _
		"	{" & vbCrLf & _
		"		var docSiteContentUrl = document.getElementById('" & ContentUrl.ClientID & "');" & vbCrLf & _
		"" & vbCrLf & _
		"		try { docSiteContentUrl.value = window.frames[0].location; }" & vbCrLf & _
		"		catch (e) { }  // Access denied" & vbCrLf & _
		"" & vbCrLf & _
		"		" & GetSyncTocClientCallback() & ";" & vbCrLf & _
		"	}" & vbCrLf & _
		"" & vbCrLf & _
		"	contentFrame_FirstLoad = false;" & vbCrLf & _
		"}"

		ScriptManager.RegisterStartupScript(Page, Me.GetType(), "updateDocSitePath", script, True)
	End Sub

	Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
		ContentFrame.Attributes.Add("onload", "ContentFrame_onload(this);")

		MyBase.OnPreRender(e)
	End Sub

	Public Property ContentPath() As String Implements DaveSexton.DocProject.DocSites.IDocSiteDefault.ContentPath
		Get
			Return ContentFrame.Attributes("src")
		End Get
		Set(ByVal value As String)
			If value Is Nothing Then
				Throw New ArgumentNullException("value")
			Else
				ContentFrame.Attributes("src") = value
			End If
		End Set
	End Property

	Public ReadOnly Property SelectedTopic() As String Implements DaveSexton.DocProject.DocSites.IDocSiteDefault.SelectedTopic
		Get
			Return CType(Master, DocSite).Sidebar.TableOfContents.SelectedTopic
		End Get
	End Property
End Class